package com.tg.app.activity.device.list;

import static com.tg.app.activity.device.add.AddFragmentTabActivity.DEVICE_ADD_TYPE_AP_LOCAL;
import static com.tg.app.activity.device.add.AddFragmentTabActivity.EXT_DEVICE_ADD_TYPE;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.tange.base.toolkit.WifiAp;
import com.appbase.custom.constant.DeviceStateConstants;
import com.tange.module.camera.hub.CameraHub;
import com.tg.app.R;
import com.tg.app.activity.device.add.AddFragmentTabActivity;
import com.tg.app.util.LocalThumbnailUtils;
import com.tg.app.util.ObjectBoxUtil;
import com.tange.base.toolkit.NetworkUtil;
import com.tg.appcommon.android.TGAlertDialog;
import com.tg.data.bean.DeviceItem;

import java.util.List;


public class LocalDeviceListFragment extends DeviceListBaseFragment {

    private static final String TAG = "LocalDeviceListFragment";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        isLocalList = true;
        View v = super.onCreateView(inflater, container, savedInstanceState);
        drawMenu.setBackgroundResource(R.mipmap.icon_device_list_back);
        mAdapter.setLocal(true);
        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        getCameraList();
    }

    public void onActivityRestart() {
        getCameraList();
    }

    @Override
    protected void buildAdpater() {

            btnDisplayChanged.setVisibility(View.GONE);
            listAdapter();

    }

    @Override
    protected void onDrawerClicked() {
        getActivity().finish();
    }

    @Override
    public void getCameraList() {
        List<DeviceItem> deviceItems = ObjectBoxUtil.getDeviceItem().query().build().find();

        if (deviceItems == null) {
            Log.i(TAG, "getCameraList: deviceItems = null");
        } else {
            Log.i(TAG, "getCameraList: deviceItems.size = " + deviceItems.size());
        }

        deviceList.clear();
        if (deviceItems.size() > 0) {
            deviceList.addAll(deviceItems);
        }
        updateUI();
    }

    @Override
    public void updateUI() {
        super.updateUI();
    }

    @Override
    protected boolean onBackKeyClicked() {
        getActivity().finish();
        return true;
    }

    @Override
    public void onItemClick(int position, int type) {
        DeviceItem item = deviceList.get(position);
        item.is_open = 1;
        item.is_online = DeviceStateConstants.DEVICE_ONLINE;
        WifiAp wifiAp = NetworkUtil.getConnectWifiSSID(getActivity());
        // wifi不对，不可以播放
        if (!TextUtils.isEmpty(wifiAp.ssid) && !wifiAp.ssid.equals(item.uuid)) {
            localDevicePlayFailed(position, item);
            return;
        }
        CameraHub.getInstance().setEnableLocalApConnect(true);
        openDevice(item, type);

    }

//    TODO: 注意上层Activity透传过来
//    @Override
//    protected void onRestart() {
//        super.onRestart();
//        EnvironmentProxy.LOCAL_LOGIN_ENABLE  = false;
//    }

    @Override
    public void onViewClick() {
        CameraHub.getInstance().setEnableLocalApConnect(true);
        Intent toAp = new Intent(getActivity(), AddFragmentTabActivity.class);
        toAp.putExtra(EXT_DEVICE_ADD_TYPE, DEVICE_ADD_TYPE_AP_LOCAL);
        startActivity(toAp);
    }

    @SuppressLint("StringFormatInvalid")
    private void localDevicePlayFailed(final int position, final DeviceItem device) {
        new TGAlertDialog(getActivity()).builder().setTitle(R.string.local_play_fail).setMessage(String.format(getString(R.string.local_play_fail_msg), device.uuid))
                .setPositiveButton(R.string.confirm, v -> {

                }).setNegativeButton(getString(R.string.local_device_delete), v -> {
                    ObjectBoxUtil.getDeviceItem().remove(device.id);
                    LocalThumbnailUtils.getInstance().deleteBitmap(device.uuid);
                    getCameraList();
                }).show();
    }


}
